package com.huanghuai.retail.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.dto.SInventoryDto;
import com.huanghuai.retail.entity.PInformation;
import com.huanghuai.retail.entity.SInventory;
import com.huanghuai.retail.service.SInventoryService;
import com.huanghuai.retail.utils.VerifyUtils;
import com.huanghuai.retail.validator.InventoryValidator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/inventory")
public class SInventoryController {
    @Autowired
    SInventoryService sInventoryService;

    /**
     * 添加产品库存信息
     *
     * @param sInventory 库存
     * @return {@link R}<{@link String}>
     */
    @PostMapping
    public R<String> addInventory(@RequestBody SInventory sInventory) {
        sInventory.setId(null);
        sInventory.setLastNumber(0);
        String verify =
                VerifyUtils.verify(InventoryValidator.class,
                        sInventory);
        if (StringUtils.isNotBlank(verify)) {
            return R.error(verify);
        }
        boolean save = sInventoryService.save(sInventory);
        if (!save) return R.error("添加失败");
        return R.success("添加成功");
    }

    /**
     * 根据库存id删除,并且当前库存为0
     *
     * @param id id
     * @return {@link R}<{@link String}>
     */
    @DeleteMapping("/{id}")
    public R<String> deleteByIdIfNumIsZero(@PathVariable("id") Long id) {
        boolean remove = sInventoryService
                .deleteIfNumIsZero(id, null, null);
        if (!remove) return R.error("删除失败");
        return R.success("删除成功");
    }

    /**
     * 根据产品id删除,并且当前库存为0
     *
     * @param productId 产品id
     * @return {@link R}<{@link String}>
     */
    @DeleteMapping("/pro/{storageId}/{productId}")
    public R<String> deleteByProIdIfNumIsZero(
            @PathVariable("storageId") Long storageId,
            @PathVariable("productId") Long productId) {
        boolean remove = sInventoryService
                .deleteIfNumIsZero(null, productId, storageId);
        if (!remove) return R.error("删除失败");
        return R.success("删除成功");
    }

    /**
     * 根据库存id更新
     *
     * @param inventory 库存
     * @return {@link R}<{@link String}>
     */
    @PutMapping
    public R<String> updateById(@RequestBody SInventory inventory) {
        // 数据校验
        String verify = VerifyUtils.verify(
                InventoryValidator.class,
                inventory);
        // 如果由错误直接return
        if (StringUtils.isNotBlank(verify) &&
                !verify.contains("该产品在仓库中已存在")) {
            return R.error(verify);
        }
        // 根据库存id更新
        boolean update = sInventoryService.updateById(inventory);
        if (!update) return R.error("更新失败");
        return R.success("更新成功");
    }

    /**
     * 根据产品id及仓库id更新产品库存信息
     *
     * @param inventory 库存
     * @return {@link R}<{@link String}>
     */
    @PutMapping("/pro")
    public R<String> updateByProId(@RequestBody SInventory inventory) {
        // 数据校验
        String verify = VerifyUtils.verify(
                InventoryValidator.class,
                inventory);
        // 存在错误则直接return
        if (StringUtils.isNotBlank(verify) &&
                !verify.contains("该产品在仓库中已存在")) {
            return R.error(verify);
        }
        // 根据产品id更新
        boolean update = sInventoryService.updateByProIdWithStorageId(inventory);
        if (!update) return R.error("更新失败");
        return R.success("更新成功");
    }

    /**
     * 根据产品id查询库存量
     *
     * @param productId 产品id
     * @return {@link R}<{@link SInventoryDto}>
     */
    @GetMapping("/pro/{id}")
    public R<List<SInventoryDto>>
    queryByProId(@PathVariable("id") Long productId) {
        List<SInventoryDto> inventoryDtoList =
                sInventoryService.getByIdWithDto(productId);
        if (inventoryDtoList.size() == 0) return R.error("当前产品无库存");
        R<List<SInventoryDto>> res = new R<>();
        res.setData(inventoryDtoList);
        res.setCode(1);
        // 产品具体信息
        Map<String, Object> info = new HashMap<>();
        // 设置当前产品总的库存量
        info.put("total", 0);
        inventoryDtoList.forEach(item -> {
            info.put("total", Integer.valueOf(
                    String.valueOf(info.get("total"))
                            + item.getNumber()));
        });
        res.setMap(info);
        return res;
    }

    /**
     * 分页查询所有产品的库存
     *
     * @param page        页面
     * @param pageSize    页面大小
     * @param productName 产品名称
     * @param storageIds  存储id
     * @param typeIds     类型id
     * @return {@link R}<{@link Page}<{@link SInventoryDto}>>
     */
    @GetMapping("/page")
    public R<Page<SInventoryDto>>
    pageDto(Integer page, Integer pageSize,
            String productName, Long[] storageIds,
            Long[] typeIds) {
        if (page == null || pageSize == null || page <= 0 || pageSize <= 0) {
            page = 1;
            pageSize = 5;
        }
        Page<SInventoryDto> pageInfo = sInventoryService
                .pageDto(page, pageSize,
                        productName, storageIds, typeIds);
        return R.success(pageInfo);
    }

    /**
     * 根据仓库id去查对应的商品id
     *
     * @param storageId
     * @return
     */
    @GetMapping("/findShopNameByStorageId")
    public R<Map<Long,String>> findShopIdByStorageId(@RequestParam("storageId") Integer storageId) {
        Map<Long,String> map = sInventoryService.findShopIdByStorageId(storageId);
        return R.success(map);
    }
}

